"""
基础表单类
"""
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SelectField, DateField, IntegerField, DecimalField, SubmitField, PasswordField
from wtforms.validators import DataRequired, Email, Length, EqualTo, NumberRange


class BaseForm(FlaskForm):
    """基础表单类，提供通用功能"""
    
    def get_errors(self):
        """获取表单错误信息"""
        errors = []
        for field, field_errors in self.errors.items():
            for error in field_errors:
                errors.append(f"{getattr(self, field).label.text}: {error}")
        return errors


class LoginForm(BaseForm):
    """登录表单"""
    username = StringField('用户名', validators=[DataRequired(message='请输入用户名')])
    password = PasswordField('密码', validators=[DataRequired(message='请输入密码')])
    submit = SubmitField('登录')


class RegisterForm(BaseForm):
    """注册表单"""
    username = StringField('用户名', validators=[DataRequired(message='请输入用户名'), Length(min=3, max=20)])
    email = StringField('邮箱', validators=[DataRequired(message='请输入邮箱'), Email(message='请输入有效的邮箱地址')])
    password = PasswordField('密码', validators=[DataRequired(message='请输入密码'), Length(min=6)])
    confirm_password = PasswordField('确认密码', validators=[
        DataRequired(message='请确认密码'), 
        EqualTo('password', message='两次输入的密码不一致')
    ])
    submit = SubmitField('注册')


class ProjectForm(BaseForm):
    """项目表单"""
    name = StringField('项目名称', validators=[DataRequired(message='请输入项目名称')])
    description = TextAreaField('项目描述')
    start_date = DateField('开始日期', validators=[DataRequired(message='请选择开始日期')])
    end_date = DateField('结束日期', validators=[DataRequired(message='请选择结束日期')])
    status = SelectField('项目状态', choices=[
        ('planning', '规划中'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('cancelled', '已取消')
    ], default='planning')
    submit = SubmitField('保存')


class TaskForm(BaseForm):
    """任务表单"""
    title = StringField('任务标题', validators=[DataRequired(message='请输入任务标题')])
    description = TextAreaField('任务描述')
    start_date = DateField('开始日期', validators=[DataRequired(message='请选择开始日期')])
    end_date = DateField('结束日期', validators=[DataRequired(message='请选择结束日期')])
    status = SelectField('任务状态', choices=[
        ('todo', '待办'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('cancelled', '已取消')
    ], default='todo')
    priority = SelectField('优先级', choices=[
        ('low', '低'),
        ('medium', '中'),
        ('high', '高')
    ], default='medium')
    submit = SubmitField('保存')


class CustomerForm(BaseForm):
    """客户表单"""
    # 基本信息
    name = StringField('客户名称', validators=[DataRequired(message='请输入客户名称')])
    customer_number = StringField('客户编号', render_kw={'readonly': True})
    
    # 客户分类(必填)
    category = SelectField('客户分类', choices=[
        ('普通客户', '普通客户'),
        ('VIP客户', 'VIP客户'),
        ('合作伙伴', '合作伙伴'),
        ('其他', '其他')
    ], validators=[DataRequired(message='请选择客户分类')])
    category_other = StringField('客户分类(其他)', description='当选择"其他"时，请填写具体分类')
    
    # 客户归属地(必填)
    location = SelectField('客户归属地', choices=[
        ('山西区域', '山西区域'),
        ('四川区域', '四川区域'),
        ('新疆区域', '新疆区域'),
        ('安徽区域', '安徽区域'),
        ('山东区域', '山东区域'),
        ('湖北区域', '湖北区域'),
        ('福建区域', '福建区域'),
        ('北京区域', '北京区域'),
        ('重庆区域', '重庆区域'),
        ('云南区域', '云南区域'),
        ('其他', '其他')
    ], validators=[DataRequired(message='请选择客户归属地')])
    location_other = StringField('客户归属地(其他)', description='当选择"其他"时，请填写具体归属地')
    
    # 客户来源(必填)
    source = SelectField('客户来源', choices=[
        ('网站', '网站'),
        ('推荐', '推荐'),
        ('展会', '展会'),
        ('电话', '电话'),
        ('其他', '其他')
    ], validators=[DataRequired(message='请选择客户来源')])
    source_other = StringField('客户来源(其他)', description='当选择"其他"时，请填写具体来源')
    
    # 客户地址(选填)
    province_id = SelectField('省份', coerce=int, description='请选择省份')
    city_id = SelectField('城市', coerce=int, description='请选择城市')
    district_id = SelectField('区县', coerce=int, description='请选择区县')
    address = TextAreaField('详细地址', description='请输入详细地址')
    
    # 业务信息
    manager_id = SelectField('负责人', coerce=int, description='请选择负责人')
    
    submit = SubmitField('保存')


class OpportunityForm(BaseForm):
    """商机表单"""
    name = StringField('商机名称', validators=[DataRequired(message='请输入商机名称')])
    customer_id = SelectField('客户', coerce=int, validators=[DataRequired(message='请选择客户')])
    expected_amount = DecimalField('预期金额', validators=[
        NumberRange(min=0, message='金额不能为负数')
    ])
    expected_close_date = DateField('预期成交日期', validators=[DataRequired(message='请选择预期成交日期')])
    description = TextAreaField('商机描述')
    stage_id = SelectField('商机阶段', coerce=int)
    probability = IntegerField('成功概率(%)', validators=[
        NumberRange(min=0, max=100, message='概率必须在0-100之间')
    ])
    submit = SubmitField('保存')


class ContactForm(BaseForm):
    """联系人表单"""
    name = StringField('姓名', validators=[DataRequired(message='请输入姓名')])
    title = StringField('职位')
    email = StringField('邮箱', validators=[Email(message='请输入有效的邮箱地址')])
    phone = StringField('电话')
    notes = TextAreaField('备注')
    submit = SubmitField('保存')


class FollowUpForm(BaseForm):
    """跟进记录表单"""
    type = SelectField('跟进类型', choices=[
        ('phone', '电话'),
        ('email', '邮件'),
        ('meeting', '会议'),
        ('visit', '拜访'),
        ('other', '其他')
    ], validators=[DataRequired(message='请选择跟进类型')])
    date = DateField('跟进日期', validators=[DataRequired(message='请选择跟进日期')])
    notes = TextAreaField('跟进内容', validators=[DataRequired(message='请输入跟进内容')])
    next_action = TextAreaField('下一步行动')
    next_date = DateField('下次跟进日期')
    submit = SubmitField('保存')